﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace LightCAD.Core
{
    public class Polyline2d : ICurve2d
    {
        private Box2d? _boundingBox;

        public Curve2dType Type => Curve2dType.Polyline2d;

        public Vector2d[] Points { get; set; }

        [JsonIgnore]
        public bool IsClosed
        {
            get {
                if(Points==null || Points.Length < 3)
                {
                    return false;
                }
                return Points[0] == Points[Points.Length-1];
            }
        }
        [JsonIgnore]
        public object Source { get; set; }

        public string Name { get; set; }


        public void Copy(ICurve2d src)
        {
            var polyline = src as Polyline2d;
            this.Points = polyline.Points.Clone<Vector2d>();
            this.Name = polyline.Name;
        }
        public ICurve2d Clone()
        {
            var newObj = new Polyline2d();
            newObj.Copy(this);
            return newObj;
        }
        object ICloneable.Clone()
        {
            return this.Clone();
        }



        public void ResetBoundingBox()
        {
            _boundingBox = null;
        }

        public Box2d BoundingBox
        {
            get
            {
                _boundingBox = new Box2d();
                if (_boundingBox == null)
                {
                    _boundingBox.Value.ExpandByPoints(Points);
                }
                return _boundingBox.Value;
            }
        }

    }
}
